/*
 *  This file is part of Spidex.
 *  
 *  Spidex is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Spidex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Spidex.  If not, see <http://www.gnu.org/licenses/>.
 */

package spidex.importer;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import spidex.DVDInfo;

/**
 *
 * @author xatrix
 */
public class ExcelReader {
    private TableSettings settings;
    private int firstRow;
    private int lastRow;
    private Set<DVDInfo> result = new HashSet<DVDInfo>();
    private HSSFWorkbook workbook = null;
    private HSSFSheet sheet = null;
    private HSSFRow row;
    
    public ExcelReader(File file, TableSettings settings,
            int firstRow, int lastRow) {
        this.settings = settings;
        this.firstRow = firstRow;
        this.lastRow = lastRow;
        
        if(firstRow < 0 || firstRow > lastRow) {
            throw new IllegalArgumentException("firstRow");
        }

        try{
            workbook = new HSSFWorkbook( new FileInputStream( file ) );
            sheet = workbook.getSheetAt(0);
        }
        catch(IOException ex) {
            System.out.println(ex.getMessage());
        }

        if(sheet.getLastRowNum() < lastRow) {
            throw new IllegalArgumentException(
                "lastRow is higher than row count in whole sheet"
            );
        }
    }
    
    public Set<DVDInfo> parse() {
        this.result.clear();
        for(int rowCount = firstRow; rowCount <= lastRow; rowCount++) {
            row = sheet.getRow(rowCount);
            parseCompilation(row);
        }
        return result;
    }
        
    private void parseCompilation(HSSFRow row) {            
        String value;
        ColumnType columnType;
        DVDInfo dvdInfo = new DVDInfo();
        DVDInfo clonedDVDInfo;
                
        for(int index=0; index < 5; index++) {
            columnType = settings.column2Type.get(index);
            value = parseCell( row.getCell( (short) index ));
            
            dvdInfo.setValue(columnType, value);
        }
            
        if(!settings.isMultipleNames()) {
            this.result.add(dvdInfo);
            return;
        }

        String[] filmNames = dvdInfo.getTitle().split(",\\s+");            
        for(int nameIndex=0; nameIndex < filmNames.length; nameIndex++ ) {
            clonedDVDInfo = dvdInfo.clone();
            clonedDVDInfo.setTitle(filmNames[nameIndex]);
            this.result.add(clonedDVDInfo);
        }
    }
    
    /**
     * Function parse content of the cell and return its string representation
     * @param cell which would be parsed
     * @return string value of content
     */
    private String parseCell(HSSFCell cell) {
        if(cell == null) {
            return "";
        }
        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            return String.valueOf( (int) cell.getNumericCellValue() );
        }
        if(cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
            return "";
        }
        if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            return cell.getRichStringCellValue().getString();
        }
        return null;
    }
        
    public static void main(String args[]) {
        ExcelReader reader = new ExcelReader(
            new File("/home/xatrix/Desktop/test.xls"), null, 45, 46 );
    } 
}

 